home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / TurboTCP 2.0.1 / TurboTCP source / UTurboTCP.cp < prev    next >
Encoding:
Text File  |  1994-07-11  |  4.5 KB  |  186 lines  |  [TEXT/MPCC]

  1. /*
  2. ** UTurboTCP.cp
  3. **
  4. **    TurboTCP support library
  5. **    Utility functions
  6. **
  7. **    Copyright © 1993-94, FrostByte Design / Eric Scouten
  8. **
  9. */
  10.  
  11. #include "UTurboTCP.h"
  12.  
  13. #include "CTCPDriver.h"
  14. #include "CTCPResolverCall.h"
  15.  
  16.  
  17. extern Boolean            gInBackground;        // TCL global: in background under MultiFinder
  18.  
  19.  
  20. //    —— opening/closing TCP services ——
  21.  
  22. /*______________________________________________________________________
  23. **
  24. ** InitTCP (static method)
  25. **
  26. **    Create a TCP driver object and initialize it. Should be called once at application startup.
  27. **    In a future version of TurboTCP, this routine will check for MacTCP and/or Open Transport
  28. **    and open the appropriate object.
  29. **
  30. */
  31.  
  32. void UTurboTCP::InitTCP()
  33.  
  34. {
  35.     if (!CTCPDriver::gTCPDriver)
  36.         new CTCPDriver;
  37. }
  38.  
  39.  
  40. /*______________________________________________________________________
  41. **
  42. ** CloseTCP (static method)
  43. **
  44. **    Close the TCP driver object and destroy it. Should be called once at application
  45. **    shut-down time.
  46. **
  47. */
  48.  
  49. void UTurboTCP::CloseTCP()
  50.  
  51. {
  52.     if (CTCPDriver::gTCPDriver)
  53.         (CTCPDriver::gTCPDriver)->Dispose();
  54. }
  55.  
  56.  
  57. //    —— processing network events ——
  58.  
  59. /*______________________________________________________________________
  60. **
  61. ** ProcessNetEvents (static method)
  62. **
  63. **    Call once at every event-loop (or frequent non-interrupt-level) to handle processing
  64. **    of TCP completions and notifications.
  65. **
  66. **        maxEventsToProcess (short):    maximum number of TCP events to process this time
  67. **        maxTicks (long):            maximum ticks to spend in this loop
  68. **                                    positive values:    use value “as is”
  69. **                                    zero:            use intelligent default
  70. **                                    negative value:        use value with compensation
  71. **                                                        for background activities
  72. */
  73.  
  74. void UTurboTCP::ProcessNetEvents(short maxEventsToProcess, long maxTicks)
  75.  
  76. {
  77.     static long        lastTickCount = 0;            // tick count last time through routine
  78.     long            stopTickCount;                // tick count to stop processing events
  79.     long            defaultNetTicks;            // how long are we typically in net event loop
  80.     long            defaultNetWait;                // how long to delay before each net event loop
  81.  
  82.  
  83.     // if no TCP driver, quit now
  84.  
  85.     if (!CTCPDriver::gTCPDriver)
  86.         return;
  87.  
  88.  
  89.     // decide how long we can go through net event loop
  90.  
  91.     if (maxEventsToProcess < 1)
  92.         maxEventsToProcess = 100;
  93.     if (lastTickCount == 0)
  94.         lastTickCount = LMGetTicks();
  95.  
  96.     if (!gInBackground) {
  97.         defaultNetTicks = 45;                            // spend up to 3/4 second on net events
  98.         defaultNetWait = 0;                                //    if in foreground
  99.     }
  100.     else {
  101.         defaultNetTicks = 40;                            // consume less time in background
  102.         defaultNetWait = 3;                                //    and give other apps more chance to
  103.                                                     //    do meaningful work
  104.     }
  105.     
  106.     
  107.     // adjust time in net event loop to other CPU activity
  108.     
  109.     if (maxTicks < 1) {
  110.         long elapsedTicks = LMGetTicks() - lastTickCount;        // long time, no see?
  111.  
  112.         if (elapsedTicks < defaultNetWait)                    // ensure that other apps can do meaningful
  113.             return;                                    //    stuff before we hog the CPU again
  114.  
  115.         if (maxTicks < 0)                                // negative maxTicks values will override
  116.             defaultNetTicks = -maxTicks;                    //    earlier decision of how much time
  117.  
  118.         maxTicks = defaultNetTicks - elapsedTicks;            // reduce time if CPU is otherwise busy
  119.  
  120.         if (maxTicks < 2)                                // make sure we get at least a little time
  121.             maxTicks = 2;
  122.         if (maxTicks > defaultNetTicks)                        // not sure how this would happen, but …
  123.             maxTicks = defaultNetTicks;
  124.     }
  125.     
  126.     
  127.     // record the current time
  128.     
  129.     lastTickCount = LMGetTicks();
  130.     stopTickCount = lastTickCount + maxTicks;
  131.  
  132.  
  133.     // loop through async event queue (until queue is empty or time is up)
  134.     
  135.     while ((maxEventsToProcess-- > 0) && (LMGetTicks() < stopTickCount)) {
  136.         if (!(CTCPDriver::gTCPDriver)->ProcessOneNetEvent())
  137.             break;                        // no more events to process
  138.     }
  139.     
  140.     
  141.     // rerecord current time (in case net event processing was incredibly slow)
  142.  
  143.     lastTickCount = LMGetTicks();
  144.  
  145. }
  146.  
  147.  
  148. //    —— IP address utilities ——
  149.  
  150. /*______________________________________________________________________
  151. **
  152. ** GetLocalIPAddr (static method)
  153. **
  154. **    Get the local machine’s IP address.
  155. **
  156. **        return (unsigned long):    the local IP address
  157. **
  158. */
  159.  
  160. unsigned long UTurboTCP::GetLocalIPAddr()
  161.  
  162. {
  163.     if (CTCPDriver::gTCPDriver)
  164.         return (CTCPDriver::gTCPDriver)->GetIPAddr();
  165.     else
  166.         return 0L;
  167. }
  168.  
  169.  
  170. /*______________________________________________________________________
  171. **
  172. ** DoAddrToStr (static method)
  173. **
  174. **    Convert an IP address to dotted decimal format.
  175. **
  176. **        theIPaddr (unsigned long):    the IP address to convert
  177. **        theString (char*):        pointer to a buffer for the string (16 chars)
  178. **
  179. */
  180.  
  181. void UTurboTCP::DoAddrToStr(unsigned long theIPaddr, char* theString)
  182.  
  183. {
  184.     CTCPResolverCall::DoAddrToStr((ip_addr) theIPaddr, theString);
  185. }
  186.